package com.moon.controller;

import com.moon.dao.TOrderMapper;
import com.moon.entity.TAuction;
import com.moon.entity.TOrder;
import com.moon.entity.TUser;
import com.moon.feign.UserFeign;
import com.moon.utils.IdWorker;
import com.moon.utils.UserUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    @Qualifier("idWorker1")
    private IdWorker idWorker1;

    @RequestMapping("/addOrder")
    public String addOrder(@RequestBody TAuction auction, HttpServletRequest request){
        System.out.println("进入addOrder方法");
        com.moon.entity.TUser user = UserUtils.getCurrUserFromHeader("token", com.moon.entity.TUser.class, request);
        System.out.println("user:"+user.getAccount());
        System.out.println("auction的id："+auction.getId());

        //生成订单
        TOrder order = new TOrder();
        order.setId(idWorker1.nextId());
        order.setAuctionId(auction.getId());
        order.setSellerId(user.getId());
        order.setBuyerId(auction.getBuyerId());
        order.setOrderPrice(auction.getTopPrice());
        order.setExtraPrice(auction.getTopPrice().multiply(new BigDecimal(0.03)));
        order.setFinalPrice(auction.getTopPrice().subtract(auction.getSecurityPrice()));
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setStatus(0);
        tOrderMapper.insert(order);
        Map<String, Object> map = new HashMap<>();
        Long sellerId = order.getSellerId();
        String sellerEmail=userFeign.getemail(sellerId);
        map.put("userEmail", user.getEmail());
        map.put("sellerEmail", sellerEmail);
        System.out.println("userEmail:"+map.get("userEmail"));
        System.out.println("sellerEmail:"+map.get("sellerEmail"));
        //通知买家和卖家
        rabbitTemplate.convertAndSend("sendEmailExchange", null, map);

        return "success";
    }

//  查询我的订单（我是作为买家）
    @RequestMapping("selectMyOrder")
    public List<TOrder> selectMyOrder(HttpServletRequest request){
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, request);
        TOrder order = new TOrder();
        order.setBuyerId(user.getId());
        List<TOrder> tOrders = tOrderMapper.select(order);
        return tOrders;
    }

//  查询我的订单（我是作为卖家）
    @RequestMapping("selectSellerOrder")
    public List<TOrder> selectSellerOrder(HttpServletRequest request){
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, request);
        TOrder order = new TOrder();
        order.setSellerId(user.getId());
        List<TOrder> tOrders = tOrderMapper.select(order);
        return tOrders;
    }
}
